<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class DownerPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/*
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("READ");
		if (!$_SESSION['user_id'])
			errorpage(6);
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		$where = "ITEM.item_id = {$page->id} AND ITEM.item_id = DOWNLOAD.item_id";

		$sql = db_query_select(array(array('*'),array('*')), array('ITEM','DOWNLOAD'), $where);
		$sqlresult = db_do_query($page, $sql);

		if (db_num_rows($sqlresult) !=1)
			errorpage(404);

		$download = db_fetch_array($sqlresult);

		if ($download['deleted'])
			errorpage(404);

		db_do_query($page, db_query_update(array('downloads', 'member_since', 'last_visit'), array('downloads+1', 'member_since', 'last_visit'), "MEMBER", "user_id = {$_SESSION['user_id']}"));
		db_do_query($page, db_query_update(array('downloaded'), array('downloaded+1'), "DOWNLOAD", "item_id = {$page->id}"));

		redirect("/down/{$download['filename']}");
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress() {}
	//TODO: is this method still in use?
}

?>
